package com.ruoyi.project.module.xj;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.HeEventBasic;
import com.ruoyi.project.domain.HeEventReport;
import com.ruoyi.project.domain.HeReportEvent;
import com.ruoyi.project.service.IHeEventBasicService;
import com.ruoyi.project.service.IHeEventFlowService;
import com.ruoyi.project.service.IHeEventReportService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;


/**
 * 事件基本信息Controller
 * 
 * @author ruoyi
 * @date 2023-09-03
 */
@RestController
@RequestMapping("/xj/basic")
public class XjHeEventBasicController extends BaseController
{
    @Autowired
    private IHeEventBasicService heEventBasicService;
    @Autowired
    private IHeEventReportService heEventReportService;
    @Autowired
    private IHeEventFlowService heEventFlowService;

    /**
     * 查询事件基本信息列表
     */
    /*@RequiresPermissions("project:basic:list")*/
    @GetMapping("/list")
    public TableDataInfo list(HeEventBasic heEventBasic)
    {
        startPage();
        List<HeEventBasic> list = heEventBasicService.selectHeEventBasicList(heEventBasic);
        return getDataTable(list);
    }

    /**
     * 导出事件基本信息列表
     */
   /* @RequiresPermissions("project:basic:export")*/
    @Log(title = "事件基本信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HeEventBasic heEventBasic)
    {
        List<HeEventBasic> list = heEventBasicService.selectHeEventBasicList(heEventBasic);
        ExcelUtil<HeEventBasic> util = new ExcelUtil<HeEventBasic>(HeEventBasic.class);
        util.exportExcel(response, list, "事件基本信息数据");
    }

    /**
     * 获取事件基本信息详细信息
     */
   /* @RequiresPermissions("project:basic:query")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        //新建heReportEvent对象
        HeReportEvent heReportEvent=new HeReportEvent();
        //新建heEventBasic对象
        HeEventBasic heEventBasic=new HeEventBasic();
        //通过id在basic表中查询的该条数据
        HeEventReport heEventReport= heEventReportService.selectHeEventReportById(id);
        //这个是在basic表中查到对应的report_event_number字段的值并将这个值在report表中进行查询
        heEventBasic.setReportEventNumber(heEventReport.getReportEventNumber());
        List<HeEventBasic> list = heEventBasicService.selectHeEventBasicList(heEventBasic);
        //健壮性判断
        if(list.size()>0){
            heEventBasic=list.get(0);
        }
        //将这两条数据放在那个domain对象中(分别放进去)
        heReportEvent.setHeEventBasic(heEventBasic);
        heReportEvent.setHeEventReport(heEventReport);
        //返回给前台数据
        return success(heReportEvent);
    }

    /**
     * 新增事件基本信息
     */
   /* @RequiresPermissions("project:basic:add")
    @Log(title = "事件基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HeEventBasic heEventBasic)
    {
        return toAjax(heEventBasicService.insertHeEventBasic(heEventBasic));
    }
    *//**
     * 新增事件基本信息
     */
    /*@RequiresPermissions("project:basic:add")*/
    @Log(title = "事件基本信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HeReportEvent heReportEvent)
    {
        //先新增report表
        //获取登录人的userid
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        //用于流转表
        String context=heReportEvent.getHeEventReport().getNote1();
        context=sysUser.getNickName()+"创建了"+context;
        //将note1置空防止脏数据
        heReportEvent.getHeEventReport().setNote1(null);
        heReportEvent.getHeEventReport().setReportEventState("01");
        heReportEvent.getHeEventReport().setReviewEventType("01");
        heReportEvent.getHeEventReport().setReportEventNumber(DateUtils.getNowDate().getTime());
        //新加部分
        //将report表里的ReportEventNumber加入basic表的ReportEventNumber中
        //这里在邵博块的反显中需要用到,避免事件信息不全导致的报错
        heReportEvent.getHeEventBasic().setReportEventNumber(heReportEvent.getHeEventReport().getReportEventNumber());
        //---------分割线------------
        heReportEvent.getHeEventReport().setReportFiller(sysUser.getNickName());
        heReportEvent.getHeEventReport().setReportFillerId(sysUser.getUserId());
        heReportEvent.getHeEventReport().setReportDepartment(String.valueOf(sysUser.getDeptId()));
        //将登录用户姓名注入到report的report_filler字段中
        heReportEvent.getHeEventReport().setReportFiller(sysUser.getNickName());
        //将登录用户工号注入到report的report_filler字段中
        heReportEvent.getHeEventReport().setReportFillerId(sysUser.getUserId());
        //将创建时间注入到report表的creation_time字段中
        heReportEvent.getHeEventReport().setCreationTime(DateUtils.getNowDate());

        //将状态改成未上报
        heReportEvent.getHeEventReport().setReportStatus("01");
        heReportEvent.getHeEventReport().setHandlingStatus("01");
        //将事件上报状态变成03
        heReportEvent.getHeEventReport().setReportEventState("01");
        heEventReportService.insertHeEventReport(heReportEvent.getHeEventReport());
        //查询已新增到report表的那条数据
        List<HeEventReport> list = heEventReportService.selectHeEventReportList(heReportEvent.getHeEventReport());
        //将新增的report表的id注入到basic表的report_event_id字段
        heReportEvent.getHeEventBasic().setReportEventId(heReportEvent.getHeEventReport().getId());
        heReportEvent.getHeEventFlow().setProcessOperatorId(sysUser.getUserId());
        heReportEvent.getHeEventFlow().setProcessOperatorName(sysUser.getNickName());
        heReportEvent.getHeEventFlow().setProcessOperateTime(DateUtils.dateTime());
        //将创建时间注入到basic表的creation_time字段中
        heReportEvent.getHeEventBasic().setCreationTime(DateUtils.getNowDate());
        //同时注入到flow表
        //这个是将report表的id注入到flow的report_event_id字段
        heReportEvent.getHeEventFlow().setReportEventId(heReportEvent.getHeEventReport().getId());
        //这个是将report表的note1也就是事件类型注入到flow的process_stream_content字段
        heReportEvent.getHeEventFlow().setProcessStreamContent(context);
        //同时注入到flow表
        //这个是将report表的note1也就是事件类型注入到flow的process_stream_content字段在后台拼接了一下
        //将创建时间注入到flow表的creation_time字段中
        heReportEvent.getHeEventFlow().setCreationTime(DateUtils.getNowDate());
        //新增flow表
        heEventFlowService.insertHeEventFlow(heReportEvent.getHeEventFlow());
        //新增basic表
        return toAjax(heEventBasicService.insertHeEventBasic(heReportEvent.getHeEventBasic()));
    }


    /**
     * 修改事件基本信息
     */
   /* @RequiresPermissions("project:basic:edit")*/
    @Log(title = "事件基本信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HeReportEvent heReportEvent)
    {
        //传回一个拼接两个表的对象
        //获得basic表更新数据（反显时已经存在id，无需其他操作）
        HeEventBasic heEventBasic=heReportEvent.getHeEventBasic();
        //更新
        heEventBasicService.updateHeEventBasic(heEventBasic);
        //获得report表更新数据（反显时已经存在id，无需其他操作）
        HeEventReport heEventReport=heReportEvent.getHeEventReport();
        //更新
        return toAjax(heEventReportService.updateHeEventReport(heEventReport));
    }

    /**
     * 删除事件基本信息
     */
    /*@RequiresPermissions("project:basic:remove")*/
    @Log(title = "事件基本信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(heEventBasicService.deleteHeEventBasicByIds(ids));
    }
}
